package com.jason.www.index;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Objects;

@Data
@Document(indexName = "movies",type = "movie")
public class Movie implements Serializable {
    public static final String ID = "id";
    public static final String TITLE = "title";
    public static final String DIRECTOR = "director";
    public static final String YEAR = "year";
    public static final String YEAR_TIME_STAMP = "yearTimeStamp";

    @Id
    private Long id;
    private String title;
    private String director;
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime year;
    private List<String> genres;

    //以下字段用于查询

    private Long yearTimeStamp;
    private String sortField;
    private Boolean sortDesc;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime yearStart;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime yearEnd;
    private Integer page = 0;
    private Integer pageSize = 5000;

    public Long getYearTimeStamp() {
        if (Objects.nonNull(year)) {
            return year.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        } else {
            return null;
        }
    }

    public Long getYearStartTimeStamp() {
        if (Objects.nonNull(yearStart)) {
            return yearStart.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        } else {
            return null;
        }
    }

    public Long getYearEndTimeStamp() {
        if (Objects.nonNull(yearEnd)) {
            return yearEnd.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        } else {
            return null;
        }
    }
}
